import {
  CoolController,
  BaseController,
  TagTypes,
  CoolUrlTag,
} from '@cool-midway/core';
import { Body, Inject, Post } from '@midwayjs/core';
import { GoodsPromoMzService } from '../../service/promo_mz.service';
import { GoodsPromoTcService } from '../../service/promo_tc.service';
import { GoodsPromoMsService } from '../../service/promo_ms.service';
import { GoodsPromoMjService } from '../../service/promo_mj.service';

/**
 * 促销明细前端
 */
@CoolUrlTag({
  key: TagTypes.IGNORE_TOKEN,
  value: ['getBuyGiftDetails', 'getComboDetails', 'getSeckillDetails', 'getDiscountDetails', 'checkSeckillStock'],
})
@CoolController()
export class AppGoodsPromoDetailController extends BaseController {
  @Inject()
  goodsPromoMzService: GoodsPromoMzService;

  @Inject()
  goodsPromoTcService: GoodsPromoTcService;

  @Inject()
  goodsPromoMsService: GoodsPromoMsService;

  @Inject()
  goodsPromoMjService: GoodsPromoMjService;

  @Post('/getBuyGiftDetails')
  async getBuyGiftDetails(@Body() params: { billNo?: string; goodsId?: string }) {
    if (params.billNo) {
      return this.ok(await this.goodsPromoMzService.getByBillNo(params.billNo));
    }
    if (params.goodsId) {
      return this.ok(await this.goodsPromoMzService.getByGoodsId(params.goodsId));
    }
    return this.ok(await this.goodsPromoMzService.getAllActiveDetails());
  }

  @Post('/getComboDetails')
  async getComboDetails(@Body() params: { billNo?: string; goodsId?: string }) {
    if (params.billNo) {
      return this.ok(await this.goodsPromoTcService.getByBillNo(params.billNo));
    }
    if (params.goodsId) {
      return this.ok(await this.goodsPromoTcService.getByGoodsId(params.goodsId));
    }
    return this.ok(await this.goodsPromoTcService.getAllActiveDetails());
  }

  @Post('/getSeckillDetails')
  async getSeckillDetails(@Body() params: { billNo?: string; goodsId?: string }) {
    if (params.billNo) {
      return this.ok(await this.goodsPromoMsService.getByBillNo(params.billNo));
    }
    if (params.goodsId) {
      return this.ok(await this.goodsPromoMsService.getByGoodsId(params.goodsId));
    }
    return this.ok(await this.goodsPromoMsService.getAllActiveDetails());
  }

  @Post('/getDiscountDetails')
  async getDiscountDetails(@Body() params: { billNo?: string; totalAmount?: number }) {
    if (params.billNo && params.totalAmount !== undefined) {
      return this.ok(await this.goodsPromoMjService.calculateDiscount(params.billNo, params.totalAmount));
    }
    if (params.billNo) {
      return this.ok(await this.goodsPromoMjService.getDiscountConditions(params.billNo));
    }
    return this.ok(await this.goodsPromoMjService.getAllActiveDetails());
  }

  @Post('/checkSeckillStock')
  async checkSeckillStock(@Body() params: { billNo: string; goodsId: string; quantity: number }) {
    return this.ok(await this.goodsPromoMsService.checkSeckillStock(params.billNo, params.goodsId, params.quantity));
  }

  @Post('/calculateComboPrice')
  async calculateComboPrice(@Body() params: { billNo: string }) {
    return this.ok(await this.goodsPromoTcService.calculateComboPrice(params.billNo));
  }
} 